#include <linux/mm.h>
#include <linux/head.h>

unsigned long paging_init(unsigned long start_mem, unsigned long end_mem) {
    unsigned long *pg_dir;
    unsigned long *pg_table;
    unsigned long tmp;
    unsigned long address;

    start_mem = PAGE_ALIGN(start_mem);
    address = PAGE_OFFSET;
    pg_dir = swapper_pg_dir;
    pg_dir[0] = 0;               // 取消低地址的映射
    pg_dir += PAGE_OFFSET >> 22; // 定位到内核的页表
    // 遍历页目录项
    while (address < end_mem) {
        tmp = *pg_dir;
        if (!tmp) {
            tmp = __pa(start_mem) | PAGE_TABLE;
            start_mem += PAGE_SIZE;
        }
        *pg_dir = tmp;
        pg_dir++;
        pg_table = (unsigned long *)__va(tmp & PAGE_MASK);
        // 遍历页表项
        for (tmp = 0; tmp < PTRS_PER_PAGE; tmp++, pg_table++) {
            if (address < end_mem)
                *pg_table = __pa(address) | PAGE_SHARED;
            else
                *pg_table = 0;
            address += PAGE_SIZE;
        }
    }
    invalidate(); // 刷新页目录
    return start_mem;
}

